package com.yc.service.impl;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.yc.entity.MajorInfo;
import com.yc.mapper.MajorInfoMapper;
import com.yc.service.MajorInfoService;

import java.awt.image.RescaleOp;
import java.util.List;

/**
 * 专业信息;(major_info)表服务实现类
 * @date : 2023-6-28
 */
@Service
public class MajorInfoServiceImpl implements MajorInfoService{
    @Autowired
    private MajorInfoMapper majorInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MajorInfo queryById(Integer id){
        return majorInfoMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param majorInfo 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    @Override
    public Page<MajorInfo> paginQuery(MajorInfo majorInfo, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<MajorInfo> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(majorInfo.getMname())){
            queryWrapper.eq(MajorInfo::getMname, majorInfo.getMname());
        }
        if(StrUtil.isNotBlank(majorInfo.getMshort())){
            queryWrapper.eq(MajorInfo::getMshort, majorInfo.getMshort());
        }
        //2. 执行分页查询
        Page<MajorInfo> pagin = new Page<>(current , size , true);
        IPage<MajorInfo> selectResult = majorInfoMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }

    /**
     * 新增数据
     *
     * @param majorInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MajorInfo insert(MajorInfo majorInfo){
        majorInfoMapper.insert(majorInfo);
        return majorInfo;
    }

    /**
     * 更新数据
     *
     * @param majorInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MajorInfo update(MajorInfo majorInfo){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<MajorInfo> chainWrapper = new LambdaUpdateChainWrapper<MajorInfo>(majorInfoMapper);
        if(StrUtil.isNotBlank(majorInfo.getMname())){
            chainWrapper.eq(MajorInfo::getMname, majorInfo.getMname());
        }
        if(StrUtil.isNotBlank(majorInfo.getMshort())){
            chainWrapper.eq(MajorInfo::getMshort, majorInfo.getMshort());
        }
        //2. 设置主键，并更新
        chainWrapper.set(MajorInfo::getId, majorInfo.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(majorInfo.getId());
        }else{
            return majorInfo;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id){
        int total = majorInfoMapper.deleteById(id);
        return total > 0;
    }

    /**
     * 根据专业简称查id
     * @param mshort
     * @return
     */
    public Integer selectIdByMajor(String mshort){
        return majorInfoMapper.selectIdByMajor(mshort);
    }

    /**
     * 查询专业名称
     * @return
     */
    public List<String> queryMname(){
        List<String> list = majorInfoMapper.queryMname();
        return list;
    }

    /**
     * 查班级
     * @param mname
     * @param year
     * @return
     */
    @Override
    public List<String> selectClass(String mname, String year) {
        List<String> list = majorInfoMapper.selectClass(mname, year);
        return list;
    }
}
